export function getImgSrc(richtext, num = 3) {
	let imgList = []
	richtext.replace(/<img[^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
		imgList.push(capture)
	});
	imgList = imgList.slice(0, num)
	return imgList;
}

export function giveName(item) {
	return item.user_id[0].nickname ? item.user_id[0].nickname : item.user_id[0].username
}

export function giveSrc(item) {
	return item.user_id[0]?.avatar_file?.url ?? '../../static/logo.png'
}

const db = uniCloud.database();
const utilsObj = uniCloud.importObject("utilsObj",{
	customUI:true
})

export async function likeFunc(artID) {
	let flag = await db.collection("quanzi_like")
		.where(`article_id == "${artID}" && user_id == $cloudEnv_uid`).count()
	console.log(flag)
	if (flag.result.total) {
		db.collection("quanzi_like")
			.where(`article_id == "${artID}" && user_id == $cloudEnv_uid`).remove()
		utilsObj.opreator("quanzi_article", "like_count", artID, -1)
	} else {
		db.collection("quanzi_like").add({
			article_id: artID,
		})
		utilsObj.opreator("quanzi_article", "like_count", artID, 1)
	}
}
